import type { IQueue } from "../../types";

/**
 * @description 数组实现的队列;FIFO；时间复杂度O(n)
 * @author hzq
 * @date 2025-04-10 13:37:10
 */
class Queue<T> implements IQueue<T> {
  private items: Array<T>;

  constructor() {
    this.items = [];
  }

  dequeue(): T | undefined {
    return this.items.shift();
  }

  enqueue(...args: T[]): void {
    this.items.push(...args);
  }
  front() {
    return this.isEmpty() ? void 0 : this.items[0];
  }

  isEmpty() {
    return this.size() === 0;
  }

  size() {
    return this.items.length;
  }

  clear() {
    this.items = [];
  }

  toString() {
    return this.isEmpty() ? null : `Queue [${this.front()},...rest]`;
  }
}

export default Queue;
